<template>
  <el-main class="subject">
    <div>
      <!-- Form -->
      <el-button
        type="primary"
        @click="dialogFormVisible = !dialogFormVisible"
        round
        class="primaryButton"
       style="margin-right: 10px;width:180px"
       v-if="getRoleInSession"
        >添加住宿人员
      </el-button>

      <el-dialog
        title="添加住宿人员"
        :visible.sync="dialogFormVisible"
        width="500px"
      >
        <el-form :model="zhuForm">
          
          <el-form-item label="学号" :label-width="formLabelWidth">
            <el-select v-model="zhuForm.stuId" placeholder="请选择学号" filterable clearable>
            <el-option
              v-for="item in studentIdList"
              :key="item.stuId"
              :value="item.stuId">
            </el-option>
          </el-select>
          </el-form-item>
           <el-form-item label="楼房号" :label-width="formLabelWidth">
            <el-select v-model="zhuForm.housId" placeholder="请选择楼房号" filterable clearable>
            <el-option
              v-for="item in housIdList"
              :key="item.housId"
              :value="item.housId">
            </el-option>
          </el-select>
          </el-form-item>
           <el-form-item label="宿舍号" :label-width="formLabelWidth">
           <el-select v-model="zhuForm.dormId" placeholder="请选择宿舍号" filterable clearable>
            <el-option
              v-for="item in dormIdList"
              :key="item.dormId"
              :value="item.dormId">
            </el-option>
          </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="addZhu()">确 定</el-button>
        </div>
      </el-dialog>

      <el-dialog
        title="修改住宿人员"
        :visible.sync="dialogFormVisibleByUpdate"
        width="500px"
      >
         <el-form :model="zhuForm">
          
          <el-form-item label="学号" :label-width="formLabelWidth">
            <el-select v-model="zhuForm.stuId" placeholder="请选择学号" filterable clearable>
            <el-option
              v-for="item in studentIdList"
              :key="item.stuId"
              :value="item.stuId">
            </el-option>
          </el-select>
          </el-form-item>
           <el-form-item label="楼房号" :label-width="formLabelWidth">
            <el-select v-model="zhuForm.housId" placeholder="请选择楼房号" filterable clearable>
            <el-option
              v-for="item in housIdList"
              :key="item.housId"
              :value="item.housId">
            </el-option>
          </el-select>
          </el-form-item>
           <el-form-item label="宿舍号" :label-width="formLabelWidth">
           <el-select v-model="zhuForm.dormId" placeholder="请选择宿舍号" filterable clearable>
            <el-option
              v-for="item in dormIdList"
              :key="item.dormId"
              :value="item.dormId">
            </el-option>
          </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="updateZhu()">确 定</el-button>
        </div>
      </el-dialog>

      <el-button
        type="primary"
        round
        class="primaryButton"
        style="width: 150px"
        @click="getAllZhu()"
        >查询全部住宿人员</el-button
      >

      <el-input
        placeholder="
          住宿号
        "
        v-model="findKeyByZId"
        style="width: 200px; margin-left: 20%"
        clearable
      >
      </el-input>
       <el-input
        placeholder="
          学号
        "
        v-model="findKeyBySId"
        style="width: 200px; margin-left: 1%"
        clearable
      >
      </el-input>
      <el-button
        icon="el-icon-search"
        circle
        type="primary"
        style="margin-left: 10px"
        @click="getZhuByName()"
      ></el-button>
    </div>

    <div>
      <el-dialog title="详情信息" :visible.sync="detailInformationShow">
      <el-table :data="zhuData" v-loading="loading">
        <el-table-column property="zhuId" label="住宿号" width="150"></el-table-column>
        <el-table-column property="stuId" label="学号" width="150"></el-table-column>
        <el-table-column property="stuName" label="学生姓名" width="200"></el-table-column>
        <el-table-column property="housId" label="楼房号" width="200"></el-table-column>
        <el-table-column property="dormId" label="宿舍号" width="150"></el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button @click="detailInformationShow = false">取 消</el-button>
        <el-button type="primary" @click="detailInformationShow = false">确 定</el-button>
      </div>
    </el-dialog>
    </div>

    <div style="margin-top: 30px;width: 1200px">
      <el-table
        :data="zhuTableData"
        border
         :style="styleByRole"
        v-loading="loading"
        tooltip-effect="dark"
        @selection-change="selectionChange($event)"
        ref="multipleTable"
      >
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column fixed prop="zhuId" label="住宿id" width="150">
        </el-table-column>
        <el-table-column prop="stuId" label="学号" width="120">
        </el-table-column>
         <el-table-column prop="housId" label="楼房号" width="120">
        </el-table-column>
         <el-table-column prop="dormId" label="宿舍号" width="120">
        </el-table-column>

        <el-table-column fixed="right" label="操作" width="200"  >
             
          <template slot-scope="scope">
             <el-button
              type="primary"
              circle
              size="small"
              @click="showZhu(scope.row)"
              >详情</el-button >
            <el-button
              @click="dialogFormVisibleShow(scope.row)"
              type="primary"
              circle
              size="small"
              v-if="getRoleInSession"
              >编辑</el-button
            >
            <el-button
              type="danger"
              circle
              size="small"
              v-if="getRoleInSession"
              @click="deleteZhu(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div style="margin-top: 10px; margin-left: 20%">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.currentPage"
        :page-sizes="page.pageSizes"
        :page-size="page.sizePage"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.pageTotal"
      >
      </el-pagination>
    </div>
  </el-main>
</template>

<script>
import { Loading } from "element-ui";
export default {
  name: "Zhu",
  data() {
    return {
      zhuData:[],
      detailInformationShow:false,
      zhuTableData: [],
      findKeyByZId: "",
      findKeyBySId:"",
      dialogFormVisible: false,
      dialogFormVisibleByUpdate: false,
      loading: true,
      multipleSelection: [],
      studentIdList:[],
      dormIdList:[],
      housIdList:[],
      // zhu表单
      zhuForm: {
        stuId: "",
        housId:"",
        dormId:"",
        
      },
      formLabelWidth: "130px",
      //单选框选择，默认选择0，id查询
      chooseRadio: 0,
      PHInFindKey: {
        firstChoose: "按照学号查询",
        secondChoose: "按照姓名模糊查询",
      },
      //匹配对应的方法，1是查询全部，2是查询单个
      eqMethod: {
        methodNum: 0,
      },
      //传输住宿人员数据的默认参数
      zhu: {
        zhuId: "0",
        stuId: "0",
        housId:"0",
        dormId:"0"
      },
      //传输page数据的默认参数
      page: {
        currentPage: 1,
        sizePage: 5,
        pageTotal: 0,
        pageSizes: [3, 5, 8],
      },
      // 传输数据的容器
      objectData: {
        zhu: {},
        page: {},
      },
    };
  },
  computed:{
    getRoleInSession(){
      return sessionStorage.getItem("rid")==="1"?true:false
    },
    styleByRole(){
       return sessionStorage.getItem("rid")==="1"?"width:800px":"width:800px;margin-left:100px"
    }
  },
  methods: {
    showZhu(zhu){
        this.detailInformationShow = true;
         this.$axios({
            method: "POST",
            headers: {
              "Content-Type": "application/json",
            },
            url: this.$globalConfig.testUrl + "zhu/getDetailInformation/",
            data: {stuId:zhu.stuId},
          }).then(
            (response) => {
              console.log("getDetailInformation请求成功了", response.data);
              this.zhuData = response.data.extend.zhuList;
              this.loading = false;
            },
            (error) => {
                console.log("请求失败了", error.message);
                this.loading = false;
            }
          );
          this.loading = true;
    },
    handleClick(a) {
      console.log(a);
    },
    //选择一次展现几个时候触发
    handleSizeChange(size) {
      console.log("handleSizeChange变换");
      this.page.sizePage = size;
      // this.getAllZhu();
      if (this.eqMethod.methodNum === 1) {
        this.getAllZhu();
      }
      if (this.eqMethod.methodNum === 2) {
        this.getZhuByName();
      }
      console.log("this.page.sizePage" + this.page.sizePage);
    },
    //当前页变化时候触发
    handleCurrentChange(currentPageNum) {
      this.page.currentPage = currentPageNum;
      if (this.eqMethod.methodNum === 1) {
        this.getAllZhu();
      }
      if (this.eqMethod.methodNum === 2) {
        this.getZhuByName();
      }
      // console.log(this.eqMethod.methodNum===1);
      // this.getAllZhu();
    },
    // 获得全部住宿人员
    getAllZhu() {
      this.eqMethod.methodNum = 1;
      console.log(
        this.$globalConfig.testUrl + "zhu/getAllZhu/" + this.objectData
      );
      this.objectData.zhu = this.zhu;
      this.objectData.page = this.page;
      console.log(this.objectData);
      this.$axios
        .post(this.$globalConfig.testUrl + "zhu/getAllZhu/", {
          page: this.page,
        })
        .then(
          (response) => {
            console.log("请求成功了", response.data);
            this.zhuTableData = response.data.extend.zhuList;
            this.page.currentPage4 = response.data.extend.currentPage;
            this.page.sizePage = response.data.extend.sizePage;
            this.page.pageTotal = response.data.extend.pageTotal;
            this.loading = false;
          },
          (error) => {
            console.log("请求失败了", error.message);
            this.loading = false;
          }
        );
    },
    //按照学号来查询
    getZhuByName() {
      this.eqMethod.methodNum = 2;
      this.zhu.zhuId = this.findKeyByZId;
      this.zhu.stuId = this.findKeyBySId;
      console.log({
        object: this.zhu,
        page: this.page,
      });
      if (this.zhu.stuId != "" && this.zhu.zhuId!="" ) {
        this.$axios
          .post(this.$globalConfig.testUrl + "zhu/getOneZhu", {
            object: this.zhu,
            page: this.page,
          })
          .then(
            (response) => {
              console.log("请求成功了", response.data);
              this.zhuTableData = response.data.extend.zhuList;
              this.page.currentPage4 = response.data.extend.currentPage;
              this.page.sizePage = response.data.extend.sizePage;
              this.page.pageTotal = response.data.extend.pageTotal;
              this.loading = false;
            },
            (error) => {
              console.log("请求失败了", error.message);
              this.loading = false;
            }
          );
      } else {
        alert("请检查您输入的条件是否为空");
      }
    },
    //添加住宿人员
    addZhu() {
      this.$axios({
        method: "POST",
        headers: {
          "Content-Type": "application/json",
        },
        url: this.$globalConfig.testUrl + "zhu/addZhu",
        data: this.zhuForm,
      }).then(
        (response) => {
          console.log("请求成功了getZhuByName", response.data);
           if(response.data.code===200){
              this.$message({
                message: '处理成功',
                type: 'success'
               });
          }else{
              this.$message.error('处理失败');
          }
          this.getAllZhu();
        },
        (error) => {
          console.log("请求失败了", error.message);
        }
      );
      
      this.dialogFormVisible = false;
    },
    //删除住宿人员
    deleteZhu(zhu) {
      console.log(zhu.rid);
      // this.zhu.zhuId = zhu.zhuId;
      // 弹窗提示框
      this.$confirm("此操作将永久删除该条信息, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$axios({
            method: "POST",
            headers: {
              "Content-Type": "application/json",
            },
            url: this.$globalConfig.testUrl + "zhu/deleteZhu",
            data: {zhuId:zhu.zhuId},
          }).then(
            (response) => {
              if (response.data.msg === "处理成功")
                this.$message({
                  type: "success",
                  message: "删除成功!",
                });
              console.log("请求成功了deleteZhu", response.data);
              this.getAllZhu();
            },
            (error) => {
              console.log("请求失败了", error.message);
            }
          );
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });

      // 将数据恢复原样
      this.zhu.rid = "";
    },
    // 展示对话框
    dialogFormVisibleShow(zhu) {
      this.zhuForm = zhu;
      this.dialogFormVisibleByUpdate = true;
    },
    // 修改住宿人员
    updateZhu() {
      console.log(this.zhuForm);
      this.$axios({
        method: "POST",
        headers: {
          "Content-Type": "application/json",
        },
        url: this.$globalConfig.testUrl + "zhu/updateZhu",
        data: this.zhuForm,
      }).then(
        (response) => {
          console.log("请求成功了updateZhu", response.data);
           if(response.data.code===200){
              this.$message({
                message: '处理成功',
                type: 'success'
               });
          }else{
              this.$message.error('处理失败');
          }
          this.getAllZhu();
        },
        (error) => {
          console.log("请求失败了", error.message);
        }
      );
      this.dialogFormVisibleByUpdate = false;
    },
    // 这里可以做多个删除
    selectionChange(val) {
      this.multipleSelection = val;
      console.log(this.multipleSelection);
      this.multipleSelection.forEach((a)=>{
        console.log(a.zhuId);
      })
    },
     getAllStudentId(){
       this.$axios
          .post(this.$globalConfig.testUrl + "student/getAllStudentId")
          .then(
          (response) => {
              this.studentIdList = response.data.extend.stuIdList;
          },
          (error) => {
              console.log("请求失败了", error.message);
          }
        );
    },
    getAllDromHId(){
          this.$axios
            .post(this.$globalConfig.testUrl + "dorm/getAllDromHId",{"dormId":this.zhuForm.dormId})
            .then(
            (response) => {
                console.log("getAllDHIdList 请求成功了", response.data);
                this.housIdList = response.data.extend.HIdList;
            },
            (error) => {
                console.log("请求失败了", error.message);
            }
        );
      
      
    },
    getAllDromDId(){
      this.$axios
          .post(this.$globalConfig.testUrl + "dorm/getAllDromDId",{"housId":this.zhuForm.housId})
          .then(
          (response) => {
              console.log("getAllDHIdList 请求成功了", response.data);
              this.dormIdList = response.data.extend.DIdList;
          },
          (error) => {
              console.log("请求失败了", error.message);
          }
        );
    },
  },
  watch:{
      zhuForm:{
					deep:true,
					handler(){
            this.getAllStudentId();
            this.getAllDromHId();
						this.getAllDromDId();
					}
				}
  },
  mounted() {
    this.getAllZhu();
    this.getAllStudentId();
    this.getAllDromDId();
    this.getAllDromHId();
    // console.log("Zhu被挂载了")
    // this.$bus.$emit('ControlHomeSpan',false);
  },
  beforeDestroy() {
    // console.log("Main被摧毁了")
  },
};
</script>

<style scoped>
.subject {
  position: absolute;
  top: 100px;
  left: 40px;
  flex: auto;
  background-color: white;
}
.primaryButton {
  width: 100px;
  height: 40px;
  font-size: 16px;
}
.inputSize {
  width: 200px;
}
</style>